package com.ruoyi.order.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.order.domain.Comment;
import com.ruoyi.order.domain.PositionRecorder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.order.domain.TripOrder;
import com.ruoyi.order.service.ITripOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 *
 * @author ruoyi
 * @date 2024-09-11
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/order/order")
public class TripOrderController extends BaseController {

    private final ITripOrderService tripOrderService;

/**
 * 查询订单管理列表
 */
@PreAuthorize("@ss.hasPermi('order:order:list')")
@GetMapping("/list")
    public TableDataInfo list(TripOrder tripOrder) {
        startPage();
        LambdaQueryWrapper<TripOrder> lqw = new LambdaQueryWrapper<TripOrder>();
                        if (StringUtils.isNotBlank(tripOrder.getOrderNum())){
                        lqw.eq(TripOrder::getOrderNum ,tripOrder.getOrderNum());
                    }
                        if (tripOrder.getUserId() != null){
                        lqw.eq(TripOrder::getUserId ,tripOrder.getUserId());
                    }
                        if (tripOrder.getDriverId() != null){
                        lqw.eq(TripOrder::getDriverId ,tripOrder.getDriverId());
                    }
                        if (tripOrder.getTripTypeId() != null){
                        lqw.eq(TripOrder::getTripTypeId ,tripOrder.getTripTypeId());
                    }
                        if (tripOrder.getOrderType() != null){
                        lqw.eq(TripOrder::getOrderType ,tripOrder.getOrderType());
                    }
                        if (tripOrder.getCouponId() != null){
                        lqw.eq(TripOrder::getCouponId ,tripOrder.getCouponId());
                    }
                        if (tripOrder.getTipNumber() != null){
                        lqw.eq(TripOrder::getTipNumber ,tripOrder.getTipNumber());
                    }
                        if (tripOrder.getEnabledOrderInfoId() != null){
                        lqw.eq(TripOrder::getEnabledOrderInfoId ,tripOrder.getEnabledOrderInfoId());
                    }
                        if (StringUtils.isNotBlank(tripOrder.getStatus())){
                        lqw.eq(TripOrder::getStatus ,tripOrder.getStatus());
                    }
        List<TripOrder> list = tripOrderService.list(lqw);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TripOrder> util = new ExcelUtil<TripOrder>(TripOrder.class);
        util.importTemplateExcel(response, "订单管理数据");
    }

    /**
     * 导入订单管理数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('order:order:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TripOrder> util = new ExcelUtil<TripOrder>(TripOrder.class);
        List<TripOrder> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            tripOrderService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TripOrder tripOrder) {
        List<TripOrder> list = tripOrderService.selectTripOrderList(tripOrder);
        ExcelUtil<TripOrder> util = new ExcelUtil<TripOrder>(TripOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(tripOrderService.selectTripOrderByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TripOrder tripOrder) {
        return toAjax(tripOrderService.insertTripOrder(tripOrder));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TripOrder tripOrder) {
        return toAjax(tripOrderService.updateTripOrder(tripOrder));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(tripOrderService.deleteTripOrderByOrderIds(orderIds));
    }
    /**
     * 根据订单号查询评论
     */
    @PreAuthorize("@ss.hasPermi('order:order:select')")
    @Log(title = "查询评论", businessType = BusinessType.SELECT)
    @GetMapping("/review/{orderId}")
    public AjaxResult getOrderReview(@PathVariable("orderId") Long orderId) {
        // 调用服务层方法，根据 orderId 获取评论内容
        List<Comment> reviews = tripOrderService.getReviewsByOrderId(orderId);

        // 判断是否查询到评论数据
        if (reviews == null || reviews.isEmpty()) {
            return AjaxResult.error("未找到相关评论");
        }

        return AjaxResult.success(reviews);
    }

    @PreAuthorize("@ss.hasPermi('order:order:select')")
    @Log(title = "查看路线", businessType = BusinessType.SELECT)
    @GetMapping("/routeLocations/{orderId}")
    public AjaxResult getOrderLine(@PathVariable("orderId") Long orderId) {
        List<PositionRecorder> positions = tripOrderService.getPositionsByOrderId(orderId);

        if (positions == null || positions.isEmpty()) {
            return AjaxResult.error("未找到相关位置信息");
        }

        return AjaxResult.success(positions);
    }


}


